// pages/activityDetail/activityDetail.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initDayObj: '',
    levelArray: [
      {
        name: '全部会员',
        level: 0
      }
    ],
    type: '',
    info: {},
    levelName: '',
    serverUrl: ''
  },
  del(){
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success: (res) => {
        if(res.confirm){
          app.globalData.post({
            url: '/activity/remove',
            data: {
              id: this.data.info.id,
              type: this.data.type
            },
            success: () => {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2];
              prevPage.setData({
                initDayObj: this.data.initDayObj
              })
              wx.navigateBack({})
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
      initDayObj: JSON.parse(options.initDayObj),
      serverUrl: app.globalData.serverUrl
    })
    app.globalData.post({
      url: '/activity/getDetail',
      data: {
        id: options.id,
        type: options.type
      },
      success: (data) => {
        let info = data.data
        info.images = JSON.parse(info.images)
        if (this.data.type == 'afterConsume'){
          info.deliverRestrict = JSON.parse(info.deliverRestrict)
          info.favorableRules = JSON.parse(info.favorableRules)
          let couponContent = []
          for (let x in info.favorableRules){
            let val = info.favorableRules[x]
            couponContent.push({
              money: x / 100,
              content: info.couponGroups[val]
            })
          }
          info.couponContent = couponContent
        }
        this.setData({
          info
        })

        app.globalData.post({
          url: '/level/get',
          data: {
            shopId: app.globalData.shopId
          },
          success: (data) => {
            let levelInfo = data.data.levelDetail ? data.data.levelDetail : []

            let convert = levelInfo.map(val => {
              return {
                name: val.name,
                level: val.level
              }
            })

            let levelName = [...this.data.levelArray, ...convert].filter(val => {
              if (this.data.type == 'deliverDirectly'){
                return val.level == info.level
              }
              if (this.data.type == 'afterConsume'){
                return val.level == info.deliverRestrict.level
              }
            })[0]['name']
            this.setData({
              levelName
            })
          }
        })


      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      initDayObj: this.data.initDayObj
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})